// pages/Searchsearch/Searchsearch.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: [],
    isShow: true,
    newlist: [],
    historyList: [],
    isCMC: false,
    index: '',
    newslist: []
  },


  toSearch(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  tosearchValue(e) {
    if (this.data.inputValue != '') {
      var searchData = wx.getStorageSync('searchData') || []
      searchData.push(this.data.inputValue)
      wx.setStorageSync('searchData', searchData)
      wx.navigateTo({
        url: `/pages/searchContent/searchContent?value=${this.data.inputValue}`,
      })
    }

  },
  getHistory() {
    var searchData = wx.getStorageSync('searchData') || []
    if (searchData.length === 0) {
      this.setData({
        isShow: true,
        isCMC: false
      })
    } else {
      this.setData({
        isShow: false,
        isCMC: true
      })
    }
    this.setData({
      newlist: searchData
    })
  },
  todel(e) {
    let index = e.currentTarget.dataset.index
    let setlist = wx.getStorageSync('searchData') || []
    setlist.splice(index, 1)
    wx.setStorageSync('searchData', setlist)
    if (setlist.length === 0) {
      this.setData({
        isShow: true,
        isCMC: false
      })
    } else {
      this.setData({
        isShow: false,
        isCMC: true
      })
    }
    this.setData({
      newlist: setlist
    })
  },
  toclean() {
    let that = this
    wx.showModal({
      title: '提示',
      cancelText: '关闭弹窗',
      confirmText: '清空',
      content: '是否清空历史记录',
      complete(res) {
        if (res.confirm) {
          var list = wx.getStorageSync('searchData');
          list = [];
          wx.setStorageSync('searchData', list);
          if (list.length === 0) {
            that.setData({
              isShow: true,
              isCMC: false
            })
          } else {
            that.setData({
              isShow: false,
              isCMC: true
            })
          }
          that.setData({
            newlist: list,
            ifShow: true
          });
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {



  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getHistory()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})